<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/date.class.php');

    require_once($config['app_dir'] . '/classes/forecasts.deamon.php');

    require_once($config['app_dir'] . '/classes/sparkline.data.php');
    require_once($config['app_dir'] . '/classes/sparkline.view.php');
    //---------------------------------------------------------------------------------------------------
	class objLastSparklineOutDeamon  {
			protected $pairs = array();
            protected $systems = array();
            protected $_sql_querys = array();

		public function __construct() {
			$this->_sqls();
		} //constructor

		public function GenerateID($value) {
            return 'last_sparklines_out__' . md5(strval($value));
        } //GenerateID

		protected function BuildPairs() {
            $this->pairs = objDataBase::GetInstance()->handler->GetAll($this->_sql_querys['pairs']);
        } //BuildPairs

		protected function BuildSystems() {
            $this->systems = objDataBase::GetInstance()->handler->GetAll($this->_sql_querys['systems']);
        } //BuildSystems

		protected function Prepare() {
			$this->BuildPairs();
            $this->BuildSystems();
		} //Prepare

        public function RenderItem($pair, $system=false){
          global $config;
            $id = $this->GenerateID($pair['TradeID'].'_'.$pair['Start'].'_'.$pair['Finish'].'_'.$system);
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineModel = new objSparkLineModel($pair['TradeID'], $pair['Start'], $pair['Finish'], $system);
            //$SparkLineModel->ExactInterval(true);
            //$SparkLineModel->StrongInterval(false);
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineView = new objSparkLineView(120, 40);
            $image_filename = $config['jpgraph']['cache_dir'] . $id . '.' . $SparkLineView->type;
            $SparkLineView->SetData($SparkLineModel->getData());
            @unlink($image_filename);
            $SparkLineView->Render($image_filename);
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineItem = new objTemplate();
            $SparkLineItem->caching = 2;
            $SparkLineItem->cache_lifetime = -1;
            $SparkLineItem->clear_cache('_SparkLineItem.tpl', $id);

            $SparkLineItem->Assign('id', $SparkLineModel->GetID());
            $SparkLineItem->Assign('pair', $pair['Pair']);
            $SparkLineItem->Assign('trade', $pair['TradeID']);
            $SparkLineItem->Assign('start', $pair['Start']);
            $SparkLineItem->Assign('finish', $pair['Finish']);
            $SparkLineItem->Assign('time', $t);
            $SparkLineItem->Assign('title', $SparkLineModel->GetPair());

            $SparkLineItem->Assign('image', $id . '.' . $SparkLineView->type);
            $SparkLineItem->Assign('t', DateConvert::Date('U'));
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineItem->Fetch('_SparkLineItem.tpl', $id);
        } //RenderItem

		public function Render() {
            $this->Prepare();
            //-------------------------------------------------------------------------------------------------------------------
            $this->systems[] = array('SystemID' => 0);

            //foreach($this->systems as $system) {
                foreach($this->pairs as $pair) {
                    $this->RenderItem($pair, $pair['SystemID']/*$system['SystemID']*/);
                } //foreach pair
            //} //foreach system
        } //Render

		protected function _sqls() {
			//parent::_sqls();
			//------------------------------------------------------------------
		    $this->_sql_querys['pairs'] =
				" SELECT ".
					" s.TradeID, ".
      				" t.Pair, ".
      				" s.TimeStart, ".
      				" s.TimeFinish, ".
                    " s.SystemID, ".
      				" FROM_UNIXTIME(s.TimeStart, '%Y-%m-%d') AS Start, ".
      				" FROM_UNIXTIME(s.TimeFinish, '%Y-%m-%d') AS Finish ".
				" FROM fa_sparklines s ".
   				" LEFT OUTER JOIN fa_trades t ON (t.TradeID=s.TradeID) ".
				" ORDER BY s.SortIndex";
            //------------------------------------------------------------------
            $this->_sql_querys['systems'] =
                'SELECT SystemID FROM fa_systems s WHERE s.Active=1 ORDER BY s.SystemID';
		} //_sqls
	}//class LastSparklineOutDeamon

    //---------------------------------------------------------------------------------------------------------------------------

    class objLastSparkLineInDeamon extends objLastForecastDeamon {

        public function GenerateID($value) {
            return 'last_sparklines__' . md5(strval($value));
        } //GenerateID

        public function ClearCache() {
          global $config;
            $path = $config['smarty']['cache_dir'];
            for($i=0; $i<2; $i++) {
                $d = dir($path);
                while (false !== ($entry = $d->read())) {
                    $str_pos = strpos($entry, 'last_sparklines__');
                    if(0===$str_pos || 3===$str_pos) {
                        @unlink($path . $entry);
                    }//if
                }//while
                $d->close();

                $path = $config['jpgraph']['cache_dir'];
            }//for
        }//ClearCache

        public function RenderItem($pair, $just_new=false, $system=false) {
          global $config;
            $id = $this->GenerateID($pair . '_' . $system);
            //---------------------------------------------------------------------------------------------------------------
            $period = $this->ChartPeriod->GetPeriod($system);
            if( empty($period) ) return false;
            $this->start = $period['startFormated'];
            $this->finish = $period['finishFormated'];
            //var_dump($this->start);
            //var_dump($this->finish);
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineModel = new objSparkLineModel($pair, $this->start, $this->finish, $system);

            $SparkLineModel->ExactInterval(true);
            $SparkLineModel->StrongInterval(false);
            $data = $SparkLineModel->getData();
            if( $just_new && !$SparkLineModel->isNewForecast() ) return false;
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineView = new objSparkLineView(120, 40);
            $image_filename = $config['jpgraph']['cache_dir'] . $id . '.' . $SparkLineView->type;
            $SparkLineView->SetData($data);
            @unlink($image_filename);
            $SparkLineView->Render($image_filename);
            //---------------------------------------------------------------------------------------------------------------
            $SparkLineItem = new objTemplate();
            $SparkLineItem->caching = 2;
            $SparkLineItem->cache_lifetime = $this->caching_time;
            $SparkLineItem->clear_cache('_SparkLineItem.tpl', $id);

            $SparkLineItem->Assign('id', $SparkLineModel->GetID());
            $SparkLineItem->Assign('trade', $pair);
            $SparkLineItem->Assign('start', $this->start);
            $SparkLineItem->Assign('finish', $this->finish);
            $SparkLineItem->Assign('time', $t);
            $SparkLineItem->Assign('title', $SparkLineModel->GetPair());

            $SparkLineItem->Assign('image', $id . '.' . $SparkLineView->type);
            $SparkLineItem->Assign('t', DateConvert::Date('U'));
            //---------------------------------------------------------------------------------------------------------------
            return $SparkLineItem->Fetch('_SparkLineItem.tpl', $id);
            //return true;
        }//RenderItem

        public function Render() {
            $this->ClearCache();

            $this->Prepare();

            $flag_empty=true;
            foreach($this->pairs_systems as $item) {
                if( $this->RenderItem($item['TradeID'], true, $item['SystemID']) )  $flag_empty = false;
            }
            if( $flag_empty ) $this->RenderItem(2400103, false, $item['SystemID']);

        } //Render
    } //class LastSparklineInDeamon
//-------------------------------------------------------------------------------------------------------
?>
